package leetcode_day._2022._202210._1120;

/**
 * 904. 水果成篮
 * 算法：滑动窗口
 *
 * @author yzh
 * @date 2022/10/17 22:37
 */
public class _17_904 {

    public int totalFruit(int[] fruits) {
        int ans = 0, n = fruits.length, tot = 0;
        int[] cnts = new int[n + 10];
        for (int i = 0, j = 0; i < n; i++) {
            if (++cnts[fruits[i]] == 1) ++tot;
            while (tot > 2) if (--cnts[fruits[j++]] == 0) --tot;
            ans = Math.max(ans, i - j + 1);
        }
        return ans;
    }

}
